package com.ggk911.pharmacy_system.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggk911.pharmacy_system.entity.DrugSale;
import com.ggk911.pharmacy_system.service.DrugSaleServiceImpl;
import com.ggk911.pharmacy_system.util.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 药品销售单控制层
 *
 * @author ggk-911
 * @version 1.0 2023/4/13 9:57
 */

@RestController
@RequiredArgsConstructor
@Slf4j
@CrossOrigin
@RequestMapping("/drugSale")
public class DrugSaleController {

    private final DrugSaleServiceImpl service;


    //GET查询
    @GetMapping("/queryAll")
    public RestResult queryAll(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size,
                               Integer userId,
                               @RequestParam(defaultValue = "") String startTime,
                               @RequestParam(defaultValue = "") String endTime,
                               @RequestParam(defaultValue = "day") String time) {
        Page<DrugSale> drugSalePage = new Page<>(page, size);
        QueryWrapper<DrugSale> wrapper = new QueryWrapper<>();

        System.out.println(time);

        wrapper.eq(userId != null, "user_id", userId);
        //这里有个坑啊， wrapper.condition 管不到其后面的参数执行，这里如果有null进来，会报空指针，就是这里DateUtil.beginOfDay(DateUtil.parse(dateSearch))
        if (!"".equals(startTime) && !"".equals(endTime) && "day".equals(time)) {
            wrapper.ge("drug_sale_create_time", DateUtil.beginOfDay(DateUtil.parse(startTime)));
            wrapper.le("drug_sale_create_time", DateUtil.endOfDay(DateUtil.parse(endTime)));
        }
        if (!"".equals(startTime) && !"".equals(endTime) && "month".equals(time)) {
            wrapper.ge("drug_sale_create_time", DateUtil.beginOfMonth(DateUtil.parse(startTime)));
            wrapper.le("drug_sale_create_time", DateUtil.endOfMonth(DateUtil.parse(endTime)));
        }
        if (!"".equals(startTime) && !"".equals(endTime) && "week".equals(time)) {
            wrapper.ge("drug_sale_create_time", DateUtil.beginOfWeek(DateUtil.parse(startTime)));
            wrapper.le("drug_sale_create_time", DateUtil.endOfWeek(DateUtil.parse(endTime)));
        }

        service.pageAd(drugSalePage, wrapper);

        return RestResult.ok("查询成功", drugSalePage);
    }

    //POST添加
    @PostMapping("/insert")
    public RestResult insert(DrugSale drugSale) {
        if (service.save(drugSale)) {
            return RestResult.ok("添加成功", drugSale.getDrugSaleId());
        }
        return RestResult.error("添加失败");
    }

    //PUT修改
    @PutMapping("/update")
    public RestResult update(DrugSale drugSale) {
        if (service.updateById(drugSale)) {
            return RestResult.ok("修改成功");
        }
        return RestResult.error("修改失败");
    }

    //DELETE删除
    @DeleteMapping("/delete")
    public RestResult delete(Integer id) {

        if (service.removeById(id)) {
            return RestResult.ok("删除成功");
        }
        return RestResult.error("删除失败");
    }
}
